package com.fortune.aop;

import com.faneui.common.utils.DateFormatUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Aspect
@Component
public class ControllerAopper {

    private static Logger logger = LoggerFactory.getLogger("");

    @Autowired
    protected HttpSession httpsession;

    private String time = DateFormatUtils.yyyyMMdd.format(new Date()) ;

    @Around(value = "@annotation(org.springframework.web.bind.annotation.RequestMapping) " +
            "&& within(com.fortune.controller.*Controller))"
            + "&&(execution(public ModelAndView indexOfAll(String)))")
    public Object aroundMethod(ProceedingJoinPoint point) {
        Object result = null;
        StringBuffer baseSb = new StringBuffer();
        MethodSignature signature = (MethodSignature) point.getSignature();
        String className = point.getTarget().getClass().getName();
        className = className.substring(className.lastIndexOf(".") + 1);
        baseSb.append(" ");
        baseSb.append(className);
        baseSb.append("#");
        baseSb.append(point.getSignature().getName()); // 方法名

        Class<?> returnType = signature.getReturnType(); // 得到方法返回类型
        baseSb.append(":");
        Object[] args = point.getArgs();
        StringBuffer reqStr = new StringBuffer();
        for (Object o : args) {
            if (o != null) {
                reqStr.append(o.toString());
            }
        }
        logger.info(baseSb.toString() + reqStr.toString());
        try {

            result =  point.proceed(); // 执行被代理方法
            if(result instanceof ModelAndView) {
                ((ModelAndView)result).addObject("time",time) ;
            }
        } catch (Throwable ex) {
            logger.error("系统异常", ex);
        }
        return result;
    }

    @Around(value = "@annotation(org.springframework.web.bind.annotation.RequestMapping) " +
            "&& within(com.fortune.controller.*.*Controller))"
            + "&&(execution(public ModelAndView indexOfAll(String)))")
    public Object aroundMethod2(ProceedingJoinPoint point) {
        Object result = null;
        StringBuffer baseSb = new StringBuffer();
        MethodSignature signature = (MethodSignature) point.getSignature();
        String className = point.getTarget().getClass().getName();
        className = className.substring(className.lastIndexOf(".") + 1);
        baseSb.append(" ");
        baseSb.append(className);
        baseSb.append("#");
        baseSb.append(point.getSignature().getName()); // 方法名

        Class<?> returnType = signature.getReturnType(); // 得到方法返回类型
        baseSb.append(":");
        Object[] args = point.getArgs();
        StringBuffer reqStr = new StringBuffer();
        for (Object o : args) {
            if (o != null) {
                reqStr.append(o.toString());
            }
        }
        logger.info(baseSb.toString() + reqStr.toString());
        try {

            result =  point.proceed(); // 执行被代理方法
            if(result instanceof ModelAndView) {
                ((ModelAndView)result).addObject("time",time) ;
            }

        } catch (Throwable ex) {
            logger.error("系统异常", ex);
        }
        return result;
    }


}
